﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Mvc;
using System.Web.Routing;
using Laugh.ViewEngine;
using Laugh.Environment;
using Autofac.Integration.Mvc;
using System.Reflection;

namespace Laugh.Example
{
    public class Global : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );
        }

        protected void Application_Start(object sender, EventArgs e)
        {
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            ViewEngines.Engines.Add(NVelocityViewEngine.Default);
            LaughStarter.Run(builder =>
            {
                var ss = AppDomain.CurrentDomain.GetAssemblies().Where(i => i.FullName.StartsWith("Laugh.")).ToArray();
                builder.RegisterControllers(ss);
            });

            DependencyResolver.SetResolver(new AutofacDependencyResolver(LaughStarter.Container));
        }

        protected void Application_End(object sender, EventArgs e)
        {
            LaughStarter.Stop();
        }
    }
}